
package com.shop.cloud.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.mall.admin.service.FreightTemplatChargeService;
import com.shop.cloud.mall.common.entity.FreightTemplat;
import com.shop.cloud.mall.admin.mapper.FreightTemplatMapper;
import com.shop.cloud.mall.admin.service.FreightTemplatService;
import com.shop.cloud.mall.common.entity.FreightTemplatCharge;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 运费模板
 *
 * @author
 * @date 2019-12-24 16:09:31
 */
@Service
@AllArgsConstructor
public class FreightTemplatServiceImpl extends ServiceImpl<FreightTemplatMapper, FreightTemplat> implements FreightTemplatService {
    private final FreightTemplatChargeService freightTemplatChargeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(FreightTemplat entity) {
        super.save(entity);
        //运费模板规则
        List<FreightTemplatCharge> freightTemplatChargeList = entity.getFreightTemplatChargeList();
        freightTemplatChargeList.stream().map(freightTemplatCharge->{
            freightTemplatCharge.setFreightTemplatId(entity.getId());
            return freightTemplatCharge;
        }).collect(Collectors.toList());
        return freightTemplatChargeService.saveBatch(freightTemplatChargeList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(FreightTemplat entity) {
        super.updateById(entity);
        //运费模板规则，先删再加
        freightTemplatChargeService.remove(Wrappers.<FreightTemplatCharge>lambdaQuery()
                .eq(FreightTemplatCharge::getFreightTemplatId, entity.getId()));
        List<FreightTemplatCharge> freightTemplatChargeList = entity.getFreightTemplatChargeList();
        freightTemplatChargeList.stream().map(freightTemplatCharge->{
            freightTemplatCharge.setFreightTemplatId(entity.getId());
            return freightTemplatCharge;
        }).collect(Collectors.toList());
        return freightTemplatChargeService.saveBatch(freightTemplatChargeList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        super.removeById(id);
        //运费模板规则
        return freightTemplatChargeService.remove(Wrappers.<FreightTemplatCharge>lambdaQuery()
                .eq(FreightTemplatCharge::getFreightTemplatId, id));
    }

    @Override
    public FreightTemplat getById(Serializable id) {
        FreightTemplat freightTemplat = super.getById(id);
        //运费模板规则
        freightTemplat.setFreightTemplatChargeList(
                freightTemplatChargeService.list(Wrappers.<FreightTemplatCharge>lambdaQuery()
                        .eq(FreightTemplatCharge::getFreightTemplatId, id))
        );
        return freightTemplat;
    }
}
